Page({
  data: {},
  onLoad({ id }) {
    this.getPassport(id)
  },

  // 1. 获取通行证数据
  async getPassport(id) {
    const { code, data: passport } = await wx.http.get(`/visitor/${id}`)
    if (code !== 10000) return wx.utils.toast('获取通行证数据失败')

    this.setData({ ...passport, id })
  },

  /**
   * 该钩子函数触发的前提:
   * 1. 页面中有button组件, 且设置了open-type="share"
   * 2. 用户点击右上角转发
   */
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?id=' + this.data.id,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },

  // 把二维码保存到本地
  async saveLocal() {
    const { url } = this.data
    // 1.1 获取图片信息
    const res = await wx.getImageInfo({
      src: url,
    })
    console.log(res.path)

    // 1.2 把图片保存到本地相册
    await wx.saveImageToPhotosAlbum({
      filePath: res.path,
    })
  },
})
